package com.play.ballen.me.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import com.play.ballen.R
import com.play.ballen.config.KVConst
import com.play.ballen.home.task.StepServiceUtil
import com.play.ballen.home.task.TaskUtil
import com.play.ballen.home.ui.MainActivity
import com.yariksoffice.lingver.Lingver
import com.yuven.appframework.manager.AppActivitysManager

class LanguageSettingDialogUtils(private val mContext: Context) {

    val dialog: Dialog = Dialog(mContext)
    private lateinit var contentView: View
    private lateinit var imgCheckCN: ImageView
    private lateinit var imgCheckEN: ImageView
    private var isCN = false
    private var currentCN = false
    var switchCallback: (() -> Unit)? = null

    fun show(currentCN: Boolean) {
        this.currentCN = currentCN
        this.isCN = currentCN
        if (!this::contentView.isInitialized) {
            contentView =
                LayoutInflater.from(mContext).inflate(R.layout.dialog_language_setting, null)
            contentView.let {

                imgCheckCN = it.findViewById(R.id.imgCheckCN)
                imgCheckEN = it.findViewById(R.id.imgCheckEN)

                it.findViewById<View>(R.id.cslCN).setOnClickListener {
                    imgCheckCN.setImageResource(R.mipmap.ic_dialog_item_check)
                    imgCheckEN.setImageResource(0)
                    isCN = true
                }
                it.findViewById<View>(R.id.cslEN).setOnClickListener {
                    imgCheckEN.setImageResource(R.mipmap.ic_dialog_item_check)
                    imgCheckCN.setImageResource(0)
                    isCN = false
                }

                it.findViewById<ImageView>(R.id.imgClose).setOnClickListener {
                    dialog.dismiss()
                }
                it.findViewById<View>(R.id.btnOk).setOnClickListener {
                    dialog.dismiss()

                    if (isCN) {
                        if (!this.currentCN) {
                            Lingver.getInstance().setLocale(mContext, KVConst.LANGUAGE_CN)
                            /*val localeListCompat = LocaleListCompat.forLanguageTags("zh")
                            AppCompatDelegate.setApplicationLocales(localeListCompat)*/
                            switchCallback?.invoke()
                        }
                    } else {
                        if (this.currentCN) {
                            Lingver.getInstance().setLocale(mContext, KVConst.LANGUAGE_EN)
                            /*val localeListCompat = LocaleListCompat.forLanguageTags("en")
                            AppCompatDelegate.setApplicationLocales(localeListCompat)*/
                            switchCallback?.invoke()
                        }
                    }
                }

                dialog.setContentView(it)
                dialog.setCanceledOnTouchOutside(false)
                dialog.window?.setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT
                )
                dialog.window?.setBackgroundDrawable(ColorDrawable())
            }
        }

        if (currentCN) {
            imgCheckCN.setImageResource(R.mipmap.ic_dialog_item_check)
            imgCheckEN.setImageResource(0)
        } else {
            imgCheckEN.setImageResource(R.mipmap.ic_dialog_item_check)
            imgCheckCN.setImageResource(0)
        }

        if (!dialog.isShowing) {
            dialog.show()
        }
    }

}